<!--
 * @Description:车辆过户厂端查询
 * @Autor: 刘诗文
 * @Date: 2020-12-08 20:46:50
 * @LastEditors: 马毅
 * @LastEditTime: 2021-04-15 17:16:28
-->
<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" :labelWidth="105" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      selectionType="single"
      :columns="columns"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :isSelectColumn="false"
      :datakey="'rows'"
      columnsRef="vehicleChangeQuerylsw"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="onRowSelectChange"
      :onSyncTableData="tableChangeFun"
    >
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-download" :click="ownerExportData">导出</AjaxButton>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="actions.visible" :title="actions.title" width="300" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <searchDearlerList :resData="this.resData" @dealerData="dealerData" @close="closeSetting" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils/index';
import searchDearlerList from './searchDearlerList';
import { getTransferList, getVehicleTransferExport, getDearlerListByTranslate } from '@/api/customerRepair/vehicleOwnerManagement/vehicleChange';
import { getNameByIds } from '@/api/repair/common';
import { isFormEmpty, notifyAction, camelCaseObject, findTableItem, findFormItem } from '@/utils';

export default {
  name: 'vehicleChangeQuery',
  components: {
    searchDearlerList
  },
  mixins: [dictionary],
  data() {
    this.selectedData = null;
    this.BaseTable = null;
    this.resData = {};
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchapi: getTransferList,
      fetchParams: { type: 'OEM', pageNum: 1, xhrAbort: true },
      actions: { visible: false, title: '', type: '' },
      dealerList: [], // 经销商列表
      dealerCode: '' // 经销商代码
    };
  },

  mounted() {
    this.BaseTable = this.$refs.table;
    this.getDearlerListByTranslate();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin'
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license'
        },
        {
          type: 'INPUT',
          label: '原车主姓名',
          fieldName: 'ownerName'
        },
        {
          type: 'INPUT',
          label: '新车主姓名',
          fieldName: 'newOwnerName'
        },
        {
          type: 'RANGE_DATE',
          label: '过户日期',
          style: { minWidth: '200px' },
          fieldName: 'transferDateBegin|transferDateEnd'
        },
        {
          type: 'INPUT',
          label: '经销商',
          fieldName: 'dealerName',
          readonly: true,
          initialValue: '',
          unitRender: props => {
            return <el-button icon="el-icon-search" onClick={this.queryDealer} />;
          },
          change: val => {
            this.dealerCode = val;
          }
        },
        {
          type: 'SELECT',
          label: '审核状态',
          fieldName: 'checkStatus',
          itemList: this.createDictList(1261)
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          // sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'OWNER_CODE',
          showOverflowTooltip: true
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.getText(props.row.OWNER_CODE)}</span>;
          }
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          width: 120,
          showOverflowTooltip: true
        },
        {
          title: '车牌号',
          dataIndex: 'license',
          width: 90,
          // sorter: true,
          // filter: true,
          showOverflowTooltip: true
          // filterType: 'input'
        },
        {
          title: '申请日期',
          dataIndex: 'creatAt',
          width: 120,
          showOverflowTooltip: true
        },
        {
          title: '审核日期',
          dataIndex: 'checkAt',
          width: 120,
          showOverflowTooltip: true
        },
        {
          title: '新车主编号',
          dataIndex: 'ownerNoNew',
          width: 150,
          showOverflowTooltip: true
        },
        {
          title: '新车主姓名',
          dataIndex: 'newOwnerName',
          width: 130,
          // sorter: true,
          // filter: true,
          showOverflowTooltip: true
          // filterType: 'input'
        },
        {
          title: '新车身份证号',
          dataIndex: 'newCertificateNo',
          width: 130,
          showOverflowTooltip: true
        },
        {
          title: '新车主手机',
          dataIndex: 'newMobile',
          width: 100,
          // sorter: true,
          // filter: true,
          showOverflowTooltip: true
          // filterType: 'input'
        },
        {
          title: '原车主编号',
          dataIndex: 'ownerNoOld',
          width: 120,
          // sorter: true,
          // filter: true,
          // filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '原车主姓名',
          dataIndex: 'ownerName',
          width: 150,
          // sorter: true,
          // filter: true,
          // filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '原车主身份证号',
          dataIndex: 'oldCertificateNo',
          width: 130,
          showOverflowTooltip: true
        },
        {
          title: '原车主手机',
          dataIndex: 'mobile',
          width: 90,
          // sorter: true,
          // filter: true,
          showOverflowTooltip: true
          // filterType: 'input'
        },
        {
          title: '操作人',
          dataIndex: 'operator',
          dictItems: [],
          // sorter: true,
          // filter: true,
          showOverflowTooltip: true
          // filterType: 'input'
        },
        {
          title: '过户时间',
          dataIndex: 'transferDate',
          // sorter: true,
          // filter: true,
          showOverflowTooltip: true
          // filterType: 'input'
        },
        {
          title: '品牌',
          dataIndex: 'brand',
          showOverflowTooltip: true,
          render: props => {
            return <span>{props.row.brandName}</span>;
          }
        },
        {
          title: '品牌名称',
          dataIndex: 'brandName',
          showOverflowTooltip: true,
          hidden: true
        },
        {
          title: '车系',
          dataIndex: 'series',
          showOverflowTooltip: true,
          render: props => {
            return <span>{props.row.seriesName}</span>;
          }
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          showOverflowTooltip: true,
          hidden: true
        },
        {
          title: '车型',
          dataIndex: 'model',
          // sorter: true,
          // filter: true,
          showOverflowTooltip: true,
          render: props => {
            return <span>{props.row.modelName}</span>;
          }
          // filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          showOverflowTooltip: true,
          hidden: true
        },
        {
          title: '审核状态',
          dataIndex: 'checkStatus',
          showOverflowTooltip: true,
          render: props => {
            return <span>{this.createDictText(props.row.checkStatus, 1261)}</span>;
          }
        },
        {
          title: '审核信息',
          dataIndex: 'checkInfo',
          showOverflowTooltip: true
        }
      ];
    },
    changeHandle(val) {
      val.type = 'OEM';
      if (val.dealerName === '') {
        this.dealerCode = val.ownerCode = '';
      } else {
        val.ownerCode = this.dealerCode;
      }
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(this.BaseTable.EXECUTE_RESET_HEIGHT);
    },
    onRowSelectChange(row) {
      if (!row.length) {
        this.selectedData = null;
      } else {
        this.selectedData = row[0];
      }
    },
    // 下载
    async ownerExportData() {
      await getVehicleTransferExport(this.fetchParams).then(res => {
        console.log(res);
        if (res.status !== 'failure') {
          exportExcelFile(res.data, '车辆过户信息');
        } else {
          notifyAction('导出失败', 'error');
        }
      });
    },
    // 打开经销商页面
    queryDealer() {
      this.actions = { visible: true, title: '经销商查询', type: 'search' };
    },
    // 获取经销商数据
    dealerData(val) {
      this.topFilterList.find(x => x.fieldName === 'dealerName').initialValue = val.dealerName;
      this.dealerCode = val.dealerCode;
      this.closeSetting();
    },
    // 关闭经销商页面
    closeSetting() {
      this.actions.visible = false;
    },
    // 查询全部经销商列表
    async getDearlerListByTranslate() {
      const res = await getDearlerListByTranslate();
      if (res.resultCode === 200) {
        if (res.errMsg) {
          notifyAction(res.errMsg, 'error');
        } else {
          this.dealerList = res.data;
        }
      }
    },
    // 翻译经销商列表
    getText(val) {
      return this.dealerList.find(x => x.dealerCode === val) ? this.dealerList.find(x => x.dealerCode === val).dealerName : '';
    },
    tableChangeFun(val) {
      let userIds = val.map(item => item.operator).join(',');
      userIds && this.queryUserInfoByIds(userIds);
    },
    // 根据用户ids获取用户列表
    async queryUserInfoByIds(userIds) {
      let res = await getNameByIds({ userIds });
      if (res.resultCode == 200) {
        let userDicts = res.data.map(item => ({ text: item.employeeName, value: item.employeeId + '' }));
        let tableTarget = findTableItem(this.columns, 'operator');
        tableTarget.dictItems = tableTarget.filterItems = userDicts;
      }
    }
  }
};
</script>
<style lang="less" scoped></style>
